#include "OperProcessWidget.h"
#include <QPainter>
#include <QDebug>

OperProcessWidget::OperProcessWidget(QWidget *parent)
    : CommonContainer(QString("操作进程表"), false, 2, parent)
{
    setupUI();
    setupConnections();
}

OperProcessWidget::~OperProcessWidget()
{
}

void OperProcessWidget::setupUI()
{
    m_mainLayout = new QGridLayout();
    m_mainLayout->setContentsMargins(0, 0, 0, 0);
    m_mainLayout->setSpacing(0);
    
    setupStageTable();
    setupTotalTable();
    
    QLabel *stageLabel = new QLabel("物料\n阶段投放");
    stageLabel->setObjectName("operSideLabel");
    stageLabel->setAlignment(Qt::AlignCenter);
    stageLabel->setFixedWidth(106);
    stageLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
    m_mainLayout->addWidget(stageLabel, 0, 0);
    m_mainLayout->addWidget(m_stageTable, 0, 1);

    QLabel *totalLabel = new QLabel("总量的\n分物料提示");
    totalLabel->setObjectName("operSideLabel");
    totalLabel->setAlignment(Qt::AlignCenter);
    totalLabel->setFixedWidth(106);
    totalLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
    m_mainLayout->addWidget(totalLabel, 1, 0);
    m_mainLayout->addWidget(m_totalTable, 1, 1);

    // 行列拉伸：两行等分高度，右列占满宽度
    m_mainLayout->setColumnStretch(0, 0);
    m_mainLayout->setColumnStretch(1, 1);
    m_mainLayout->setRowStretch(0, 1);
    m_mainLayout->setRowStretch(1, 1);

    setContentLayout(m_mainLayout);
}

void OperProcessWidget::setupStageTable()
{
    m_stageTable = new QTableWidget(3, 7);
    m_stageTable->setObjectName("stageTable");
    
    // 设置表头
    QStringList headers = {"第一阶段", "第二阶段", "第三阶段", "第四阶段", "第五阶段", "第六阶段", "第七阶段"};
    m_stageTable->setHorizontalHeaderLabels(headers);
    
    // 不使用纵向表头，行高随表格高度拉伸
    m_stageTable->verticalHeader()->setVisible(false);
    m_stageTable->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    
    // 设置表格属性（所有列等宽拉伸）
    m_stageTable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    m_stageTable->verticalHeader()->setFixedWidth(0);
    m_stageTable->setShowGrid(true);
    m_stageTable->setAlternatingRowColors(false);
    m_stageTable->setSelectionBehavior(QAbstractItemView::SelectItems);
    m_stageTable->setSelectionMode(QAbstractItemView::NoSelection);
    m_stageTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
    
    // 禁用滚动条
    m_stageTable->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    m_stageTable->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    
    // 表格随容器垂直方向拉伸
    m_stageTable->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    
    // 设置行合并
    m_stageTable->setSpan(0, 0, 4, 1);
    m_stageTable->setSpan(0, 1, 4, 1);
    m_stageTable->setSpan(0, 2, 4, 1);
    m_stageTable->setSpan(0, 3, 4, 1);
    m_stageTable->setSpan(0, 4, 4, 1);
    m_stageTable->setSpan(0, 5, 4, 1);
    m_stageTable->setSpan(0, 6, 4, 1);
    m_stageTable->setSpan(0, 7, 4, 1);
}

void OperProcessWidget::setupTotalTable()
{
    m_totalTable = new QTableWidget(3, 7);
    m_totalTable->setObjectName("totalTable");
    
    // 设置表头
    QStringList headers = {"物料名", "石灰", "石灰石", "高镁灰", "烧结矿", "", ""};
    m_totalTable->setHorizontalHeaderLabels(headers);
    
    // 不使用纵向表头，行高随表格高度拉伸
    m_totalTable->verticalHeader()->setVisible(false);
    m_totalTable->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    
    // 设置表格属性（所有列等宽拉伸）
    m_totalTable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    m_totalTable->verticalHeader()->setFixedWidth(0);
    m_totalTable->setShowGrid(true);
    m_totalTable->setAlternatingRowColors(false);
    m_totalTable->setSelectionBehavior(QAbstractItemView::SelectItems);
    m_totalTable->setSelectionMode(QAbstractItemView::NoSelection);
    m_totalTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
    
    // 禁用滚动条
    m_totalTable->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    m_totalTable->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    
    // 表格随容器垂直方向拉伸
    m_totalTable->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    // 第一列作为“假的 header”，不可编辑，样式与表头一致
    const QStringList totalRowTitles = {"计划投入量", "已投入量", "剩余投入量"};
    for (int r = 0; r < m_totalTable->rowCount(); ++r) {
        QTableWidgetItem *titleItem = new QTableWidgetItem(r < totalRowTitles.size() ? totalRowTitles[r] : QString());
        titleItem->setTextAlignment(Qt::AlignCenter);
        titleItem->setForeground(QBrush(QColor("#FFFFFF")));
        QFont f = titleItem->font();
        f.setBold(true);
        titleItem->setFont(f);
        titleItem->setBackground(QBrush(QColor("#165EC2"))); // 与 QHeaderView::section 背景一致
        titleItem->setFlags(Qt::ItemIsEnabled); // 不可编辑、不可选中、不可获得焦点
        m_totalTable->setItem(r, 0, titleItem);
    }
}

void OperProcessWidget::setupConnections()
{
    // 暂无需要连接的信号
}

void OperProcessWidget::setStageItem(int row, int column, const QString& text)
{
    if (!m_stageTable) {
        return;
    }
    if (row < 0 || row >= m_stageTable->rowCount() || column < 0 || column >= m_stageTable->columnCount()) {
        return;
    }

    QTableWidgetItem *item = m_stageTable->item(row, column);
    if (!item) {
        item = new QTableWidgetItem();
        item->setTextAlignment(Qt::AlignCenter);
        m_stageTable->setItem(row, column, item);
    }
    item->setText(text);
}

QString OperProcessWidget::stageItem(int row, int column) const
{
    if (!m_stageTable) {
        return QString();
    }
    if (row < 0 || row >= m_stageTable->rowCount() || column < 0 || column >= m_stageTable->columnCount()) {
        return QString();
    }

    QTableWidgetItem *item = m_stageTable->item(row, column);
    return item ? item->text() : QString();
}

void OperProcessWidget::setTotalItem(int row, int column, const QString& text)
{
    if (!m_totalTable) {
        return;
    }
    if (row < 0 || row >= m_totalTable->rowCount() || column < 0 || column >= m_totalTable->columnCount()) {
        return;
    }
    if (column == 0) {
        // 第一列为标题列，不允许修改
        return;
    }

    QTableWidgetItem *item = m_totalTable->item(row, column);
    if (!item) {
        item = new QTableWidgetItem();
        item->setTextAlignment(Qt::AlignCenter);
        m_totalTable->setItem(row, column, item);
    }
    item->setText(text);
}

QString OperProcessWidget::totalItem(int row, int column) const
{
    if (!m_totalTable) {
        return QString();
    }
    if (row < 0 || row >= m_totalTable->rowCount() || column < 0 || column >= m_totalTable->columnCount()) {
        return QString();
    }

    QTableWidgetItem *item = m_totalTable->item(row, column);
    return item ? item->text() : QString();
}